package article_management;
import java.sql.*;
import java.util.*;
import java.util.*;
public class ArticleApplication {
	private DTOArticle dtoArt;
	private DAOArticle daoArt;
	private ResultSet rs;
	private Scanner sc;
	Display is=new Display();
	//protected Pagination pg=new Pagination();
	protected Display dis=new Display();
	public static boolean SWrite=false;
	public static boolean SRead=false;
	public static boolean SDelete=false;
	public static boolean SUpdate=false;
	public static boolean SRestore=false;
	public static boolean SFind=false;
	public static int LastID;
	protected InputIO in=new InputIO();
	static public String shortcutString="";
	
	public ArticleApplication(){
		dtoArt=new DTOArticle();
		daoArt=new DAOArticle();
		sc=new Scanner(System.in);
		//updateData();
		displayData();
		//Design();
		//insertNewData();
		//update();
	}
	public void displayData(){
		rs=daoArt.read();
		try {
			while(rs.next()){
				getData();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void insertNewData(){
		String st[]=null;
		do{
			if(SWrite){  // Use Short cut key for Write
				st=Main.shortcutString.split("//");
				SWrite=false;
			}
			else
				st=is.displayInsertData();
			try {
				//LastID+=1;
				dtoArt=new DTOArticle(st[0],st[1],dis.getMachineTime(),st[2]);
				daoArt.write(dtoArt);
				///add new Id to ArrayList at position 0
				//Pagination.listNameFile.add(0,LastID); 
				System.out.println("Data write successfully!!!!!");
				if(!is.ContinueOrNot("Do you want to continue ?(y/n)"))break;
			} catch(Exception e){
				System.out.println("Error Write Data!!!");
			}
		}while(true);
		is.ClearScreen(4);
	}
	public void deleteData(){
		int id=0,index=0;
		do{
			if(SDelete){ 
				id=Integer.parseInt(Main.shortcutString.trim());
				SDelete=false;
			}
			else
				id=dis.InputId("Please Input Id that you want to Delete : ");
			try{
				if(id!=-1){
					index=in.CheckId(id); // Check input id exist or not
					if(index!=-1){ // if Id found return !-1
						if(is.ContinueOrNot("Do you want to Delete ?(y/n)")){
							dtoArt.setId(id);
							if(daoArt.delete(dtoArt)){ // if delete success return true...
								//Pagination.listNameFile.remove(index); // remove id by index from list file
								System.out.println("Delete Success");
							}else{
								System.out.println("Delete Not success please check agin...");
								break;
							}
						}
						else break;
					}
					else System.out.println("Id not found!!!!");
				}
				if(!is.ContinueOrNot("Do you want to continue delete ?(y/n)"))
						break;
					else continue;
			}catch(Exception e){
				System.out.println("Exception on process delete!!");
			}
		}while(true);
		is.ClearScreen(4);
	}
	public void searchData(){
		System.out.println("input keyword");
	    String keyword=sc.nextLine();
		rs=daoArt.search(keyword);
		try {
			while(rs.next()){
				getData();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void getData(){
		try{
			
			System.out.println("--------------------------------------------------------------------------");
			System.out.println("ID       :  "+rs.getInt(1));
			System.out.println("Title    :  "+rs.getString(2));
			System.out.println("Author   :  "+rs.getString(3));
			System.out.println("Date     :  "+rs.getString(4));
			System.out.println("		 	     Content ");
			System.out.println("--------------------------------------------------------------------------");
			String content=rs.getString(5);
//			if(content.length()>2000){
//				System.out.println(dis.showDetail(content, 1000));
//				System.out.println();
//				if(dis.ContinueOrNot("Do you want to show detail ?(y/Any key...)")){
//					System.out.println(content.substring(1000,content.length()));
//				}
//			}else{
//				System.out.println(content);
//			}
			
		}catch(SQLException e){}
	}
	public void updateData(){
		String field[]={"Art_Title","Art_Author","Art_Content"};
		String title="",author="",content="";
		System.out.println("Input Article ID to update");
		int id=in.inputNumber();
		System.out.println("1.Title.2.Author.3.Content");
		do{
			try{
				int keyword=in.inputNumber(); // input only number 
				if(keyword==1 || keyword==2 || keyword==3 ){
					switch(keyword){
						case 1:
							System.out.println("New Title  : ");
							title=in.input(); // update Title
							daoArt.update(field[0],title,id);
							break;
						case 2:
							System.out.print("New Author   : ");
							author=in.input(); // update Author
							daoArt.update(field[1], author,id);
							break;
						case 3:
							System.out.println("		         	    New Content ");
							System.out.println("--------------------------------------------------------------------------");
							System.out.print("  ");
							content=in.inputMultiLine();	
							daoArt.update(field[2], content,id);
							break;				
					}
				}
				else{ // choose again when you input invalid data
					System.out.print("Choose Menu: ");
				}
			}catch(Exception e){
				System.out.println("Exception on choose update field");
			}
		}while(true);
	}
	public void update(){
		int field=0;
		do{
			try{
				field=in.inputNumber(); // input only number 
				if(field==1 || field==2 || field==3 || field==4){
					String str="";
					switch(field){
						case 1:
							System.out.println("New Title  : ");
							str=in.input(); // update Title
							
							break;
						case 2:
							System.out.print("New Author   : ");
							str=in.input(); // update Author
							
							break;
						case 3:
							System.out.println("		         	    New Content ");
							System.out.println("--------------------------------------------------------------------------");
							System.out.print("  ");str=in.inputMultiLine();						
							break;
					}
				}
				else{ // choose again when you input invalid data
					System.out.print("Choose: ");
				}
			}catch(Exception e){
				System.out.println("Exception on choose update field");
			}
		}while(true);
	}
	public void Design(){
			dis.ShowSelectMenu();
	}	
	public static void main(String args[]){
		new ArticleApplication();
	}
}
